package ru.spb.nicetu.schoolproject;

import ru.spb.nicetu.schoolproject.data.*;
import ru.spb.nicetu.schoolproject.gui.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * Created by dchernyshov on 01.04.15.
 */
public class MyToolBar<ModelType extends HibernateTblModel> extends JToolBar {
    private String className;
    private ModelType tblModel;
    private JTable table;

    public MyToolBar() {}

    public MyToolBar(String className, ModelType tblModel, JTable table) {
        this.table = table;
        this.tblModel = tblModel;
        this.className = className;
        Action addData = new AddData(
                createImageIcon("../../../../images/Add.png"));
        add(new JButton(addData));
        Action removeData = new RemoveData(
                createImageIcon("../../../../images/Remove.png"));
        add(new JButton(removeData));
        Action modifyData = new ModifyData(
                createImageIcon("../../../../images/Modify.png"));
        add(new JButton(modifyData));

        Action updateData = new UpdateData(
                createImageIcon("../../../../images/Refresh.png"));
        add(new JButton(updateData));
    }


    public class AddData extends AbstractAction {
        public AddData(Icon icon) {
            putValue(Action.SMALL_ICON, icon);
            putValue(Action.SHORT_DESCRIPTION, "Добавить");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (isToolBarForTeacherDialog()){
                TeacherCard teacherCard = new TeacherCard();
                teacherCard.setVisible(true);
            }
            if (isToolBarForClassDialog()){
                ClassesCard classesCard = new ClassesCard();
                classesCard.setVisible(true);
            }
            if (isToolBarForPupilDialog()){
                PupilCard pupilCard = new PupilCard();
                pupilCard.setVisible(true);
            }
            if (isToolBarForMarkDialog()){
                MarkCard markCard = new MarkCard();
                markCard.setVisible(true);
            }
            if (isToolBarForLessonDialog()) {
                LessonCard lessonCard = new LessonCard();
                lessonCard.setVisible(true);
            }
        }
    }

    public class RemoveData extends AbstractAction {
        public RemoveData(Icon icon) {
            putValue(Action.SMALL_ICON, icon);
            putValue(Action.SHORT_DESCRIPTION, "Удалить");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (isRowSelected()) {
                if (tblModel.removeObject(table.getSelectedRow())) {
                    tblModel.updateCurrentPage();
                }
            }
        }
    }

    public class ModifyData extends AbstractAction {
        public ModifyData(Icon icon) {
            putValue(Action.SMALL_ICON, icon);
            putValue(Action.SHORT_DESCRIPTION, "Редактировать");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (isToolBarForTeacherDialog()){
                if (isRowSelected()) {
                    TeacherCard teacherCard = new TeacherCard((Teacher)tblModel.getObject(table.getSelectedRow()));
                    teacherCard.setVisible(true);
                }

            }
            if (isToolBarForClassDialog()){
                if (isRowSelected()) {
                    ClassesCard classesCard = new ClassesCard((SchoolClass)tblModel.getObject(table.getSelectedRow()));
                    classesCard.setVisible(true);
                }
             }
            if (isToolBarForPupilDialog()){
                if (isRowSelected()) {
                    PupilCard pupilCard = new PupilCard((Pupil)tblModel.getObject(table.getSelectedRow()));
                    pupilCard.setVisible(true);
                }
            }
            if (isToolBarForMarkDialog()) {
                if (isRowSelected()) {
                    MarkCard markCard = new MarkCard((Mark)tblModel.getObject(table.getSelectedRow()));
                    markCard.setVisible(true);
                }
            }
            if (isToolBarForLessonDialog()) {
                if (isRowSelected()) {
                    LessonCard lessonCard = new LessonCard((Lesson)tblModel.getObject(table.getSelectedRow()));
                    lessonCard.setVisible(true);
                }
            }
        }
    }

    public class UpdateData extends AbstractAction {
        public UpdateData(Icon icon) {
            putValue(Action.SMALL_ICON, icon);
            putValue(Action.SHORT_DESCRIPTION, "Обновить");
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            tblModel.updateCurrentPage();
        }
    }

    public ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = MyToolBar.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public boolean isToolBarForTeacherDialog() {
        if (className.equals(TblModelForTeachers.class.getName())) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean isToolBarForClassDialog() {
        if (className.equals(TblModelForClasses.class.getName())) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean isToolBarForPupilDialog() {
        if (className.equals(TblModelForPupils.class.getName())) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean isToolBarForMarkDialog() {
        if (className.equals(TblModelForMark.class.getName())) {
            return true;
        }
        else {
            return false;
        }
    }

    public boolean isToolBarForLessonDialog() {
        if (className.equals(TblModelForLessons.class.getName())) {
            return true;
        }
        else {
            return false;
        }
    }

    public void errorDialog(String error) {
        JOptionPane.showMessageDialog(null, error, "Ошибка",JOptionPane.ERROR_MESSAGE);
    }

    public boolean isRowSelected() {
        if (table.getSelectedRow() >= 0) {
            return true;
        }
        else {
            errorDialog("Выделите строку");
            return false;
        }
    }
}
